OutputCache in ASP.NWT

One of the really good features of ASP.NET is caching. With other technologies, caching usually requires some additional software or at least complex code, but with ASP.NET 2.0, it's easier than ever. You can use two different types of caching - output caching and standard caching. First of all, one of the easiest, let's start with it. In the following chapters we will use various forms of output cache instruction, as well as ASP.net cache object. read on.

In this chapter, we will take a look at the output cache instruction, which is the easiest way of caching material with ASP.NET. As you will see in our example, it does not need any code - there are only a few minor changes in the markup of the page, and you are good to go. In the next chapter, we will look at ways to use the OuputCachce directive. .

Here is a very simple example of the page, which will show the difference between the cached page and the non-cached page. Try to create a new project, and replace the default.aspx page to include the following markup:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Caching</title>
</head>
<body>
    <form id="form1" runat="server">
        <%= DateTime.Now.ToString() %>
    </form>
</body>
</html>

This is the standard standard except the line with the standard standard, it outputs the current date and time on the page. Try to run the project and reload the page twice. As you will see, the time has been refreshed on each reload. Now, for our example, add the following line as line number 2:


<%@ OutputCache duration="10" varybyparam="None" %>

Run our project again, and reload the page several times, as you will see, the time refreshes only in 10 seconds. Adding caching to your page is as easy as it is! Now, the parameter of the period is very clear - it tells the page how much seconds the content is cached. Every time a page is requested, ASP.NET checks that the page is in the cache, and if it is, whether it is finished or not it is served from the cache if it does not end - if yes , Then the page is removed from the cache and the page is generated from scratch and then placed in the cache.

Variable battery output is a required parameter of the cache command, it specifies a list of parameters that the cache should be different. For example, if you set it to "p", the cache is now based on the value of parameter p. Try changing our example like this:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache duration="10" varybyparam="p" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Caching</title>
</head>
<body>
    <form id="form1" runat="server">
        <%= DateTime.Now.ToString() %><br />
        <a href="?p=1">1</a><br />
        <a href="?p=2">2</a><br />
        <a href="?p=3">3</a><br />
    </form>
</body>
</html>

Now, let's run our example again, and try to click on the link, when you first arrive on the page, each has its own timestamp. Depending on the value of the cache parameter! You can specify several parameters, which are separated by semicolons.